home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Inne / Gry / Carnage_Contest / scripts / CC Original / weapons / Pistol.lua < prev    next >
Text File  |  2010-09-02  |  6KB  |  165 lines

  1. --------------------------------------------------------------------------------
  2. -- Weapon Pistol + Projectile Bullet
  3. -- Original Carnage Contest Weapon
  4. -- Script by DC, August 2009, www.UnrealSoftware.de
  5. --------------------------------------------------------------------------------
  6.  
  7. -- Setup Tables
  8. if cc==nil then cc={} end
  9. cc.pistol={}
  10. cc.pistol.bullet={}
  11.  
  12. -- Load & Prepare Ressources
  13. cc.pistol.gfx_wpn=loadgfx("weapons/pistol.bmp")                    -- Weapon Image
  14. setmidhandle(cc.pistol.gfx_wpn)
  15. cc.pistol.gfx_pro=loadgfx("weapons/shot.bmp")                    -- Projectile Image
  16. setmidhandle(cc.pistol.gfx_pro)
  17. cc.pistol.sfx_attack=loadsfx("pistol.wav")                        -- Attack Sound
  18.  
  19. --------------------------------------------------------------------------------
  20. -- Weapon: Pistol
  21. --------------------------------------------------------------------------------
  22.  
  23. cc.pistol.id=addweapon("cc.pistol","Pistol",cc.pistol.gfx_wpn)    -- Add Weapon
  24. cc.pistol.ammo=6                                                -- 6 Bullets
  25.  
  26. function cc.pistol.draw()                                        -- Draw
  27.     setblend(blend_alpha)
  28.     setalpha(1)
  29.     setcolor(255,255,255)
  30.     drawinhand(cc.pistol.gfx_wpn,6,0)
  31.     -- HUD ammobar
  32.     if cc.pistol.ammo-weapon_shots>0 then
  33.         hudammobar(cc.pistol.ammo-weapon_shots,cc.pistol.ammo)
  34.     end
  35.     -- HUD Crosshair
  36.     if cc.pistol.ammo-weapon_shots>0 then
  37.         hudcrosshair(7,3)
  38.     end
  39. end
  40.  
  41. function cc.pistol.attack(attack)                                -- Attack
  42.     -- Decrement timer
  43.     if weapon_timer>0 then
  44.         weapon_timer=weapon_timer-1
  45.     end
  46.     -- Attack
  47.     if weapon_shots<cc.pistol.ammo and weapon_timer<=0 and attack==1 then
  48.         -- No more weapon switching!
  49.         useweapon(0)
  50.         -- Reset Timer
  51.         weapon_timer=15
  52.         -- Attack
  53.         playsound(cc.pistol.sfx_attack)
  54.         weapon_shots=weapon_shots+1
  55.         id=createprojectile(cc.pistol.bullet.id)
  56.         projectiles[id]={}
  57.         -- Ignore collision with current player at beginning
  58.         projectiles[id].ignore=playercurrent()
  59.         -- Set initial position of projectile
  60.         projectiles[id].x=getplayerx(0)+(7*getplayerdirection(0))-math.sin(math.rad(getplayerrotation(0)))*5.0
  61.         projectiles[id].y=getplayery(0)+3+math.cos(math.rad(getplayerrotation(0)))*5.0
  62.         -- Set speed of projectile
  63.         projectiles[id].sx=math.sin(math.rad(getplayerrotation(0)))*15.0
  64.         projectiles[id].sy=-math.cos(math.rad(getplayerrotation(0)))*15.0
  65.         -- Initial movement
  66.         projectiles[id].x=projectiles[id].x-projectiles[id].sx*1.5
  67.         projectiles[id].y=projectiles[id].y-projectiles[id].sy*1.5
  68.         for i=1,3,1 do
  69.             if cc.pistol.bullet.move(id)==1 then
  70.                 break
  71.             end
  72.         end
  73.         -- Effects
  74.         recoil(3)
  75.         particle(p_muzzle,getplayerx(0)+(getplayerdirection(0)*7)+math.sin(math.rad(getplayerrotation(0)))*16,getplayery(0)+3-math.cos(math.rad(getplayerrotation(0)))*16)
  76.         particle(p_smoke,getplayerx(0)+(getplayerdirection(0)*7)+math.sin(math.rad(getplayerrotation(0)))*16,getplayery(0)+3-math.cos(math.rad(getplayerrotation(0)))*16)
  77.         particlespeed(-0.2+math.random()*0.4+getwind()*10.0,-1.0+math.random()*0.6)
  78.         particlefadealpha(0.005)
  79.         particle(p_bullet,getplayerx(0)+(getplayerdirection(0)*3),getplayery(0)+3)
  80.         -- End Turn
  81.         if (weapon_shots>=cc.pistol.ammo) then
  82.             endturn()
  83.         end
  84.     end
  85. end
  86.  
  87. --------------------------------------------------------------------------------
  88. -- Projectile: Bullet
  89. --------------------------------------------------------------------------------
  90.  
  91. cc.pistol.bullet.id=addprojectile("cc.pistol.bullet")        -- Add Projectile
  92.  
  93. function cc.pistol.bullet.draw(id)                            -- Draw
  94.     -- Setup draw mode
  95.     setblend(blend_light)
  96.     setalpha(1)
  97.     setcolor(255,255,0)
  98.     setscale(1,1)
  99.     -- Calculate projectile rotation
  100.     setrotation(math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy)))
  101.     -- Draw projectile
  102.     drawimage(cc.pistol.gfx_pro,projectiles[id].x,projectiles[id].y)
  103.     -- Draw Arrow if out of Screen
  104.     outofscreenarrow(projectiles[id].x,projectiles[id].y)
  105. end
  106.  
  107. function cc.pistol.bullet.update(id)                        -- Update
  108.     -- Wind + Gravity influence on speed
  109.     projectiles[id].sx=projectiles[id].sx+getwind()*0.02
  110.     projectiles[id].sy=projectiles[id].sy+getgravity()*0.75
  111.     -- Move
  112.     cc.pistol.bullet.move(id)
  113. end
  114.  
  115. function cc.pistol.bullet.move(id)
  116.     rot=math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy))
  117.     -- Move (in substep loop for optimal collision precision)
  118.     msubt=math.ceil(math.max(math.abs(projectiles[id].sx),math.abs(projectiles[id].sy))/3)
  119.     msubx=projectiles[id].sx/msubt
  120.     msuby=projectiles[id].sy/msubt
  121.     for i=1,msubt,1 do
  122.         projectiles[id].x=projectiles[id].x+msubx
  123.         projectiles[id].y=projectiles[id].y+msuby        
  124.         -- Collision
  125.         if collision(col3x3,projectiles[id].x+math.sin(math.rad(rot))*20,projectiles[id].y-math.cos(math.rad(rot))*20)==1 then
  126.             if terraincollision()==1 or objectcollision()>0 or playercollision()~=projectiles[id].ignore then
  127.                 -- Cause damage
  128.                 if playercollision()~=0 and playercollision()~=projectiles[id].ignore then
  129.                     playerpush(playercollision(),projectiles[id].sx/10.0,projectiles[id].sy/10.0)
  130.                     playerdamage(playercollision(),5)
  131.                     blood(projectiles[id].x+math.sin(math.rad(rot))*20,projectiles[id].y-math.cos(math.rad(rot))*20)
  132.                 elseif objectcollision()>0 then
  133.                     objectdamage(objectcollision(),5)
  134.                 end
  135.                 -- Destroy terrain
  136.                 for j=20,22,1 do
  137.                     terraincircle(projectiles[id].x+math.sin(math.rad(rot))*j,projectiles[id].y-math.cos(math.rad(rot))*j,3,0x00000000)
  138.                 end
  139.                 -- Effects
  140.                 playsound(sfx_ricochet1)
  141.                 particle(p_smoke,projectiles[id].x+math.sin(math.rad(rot))*21,projectiles[id].y-math.cos(math.rad(rot))*21)
  142.                 particlefadealpha(0.006)
  143.                 particle(p_muzzle,projectiles[id].x+math.sin(math.rad(rot))*21,projectiles[id].y-math.cos(math.rad(rot))*21)
  144.                 -- Free projectile
  145.                 freeprojectile(id)
  146.                 return 1
  147.             end
  148.         else
  149.             projectiles[id].ignore=0
  150.         end
  151.         -- Water
  152.         if (projectiles[id].y)>getwatery()+5 then
  153.             -- Effects
  154.             particle(p_waterhit,projectiles[id].x,projectiles[id].y)
  155.             if math.random(1,2)==1 then
  156.                 playsound(sfx_hitwater2)
  157.             else
  158.                 playsound(sfx_hitwater3)
  159.             end
  160.             -- Free projectile
  161.             freeprojectile(id)
  162.             return 1
  163.         end
  164.     end
  165. end